/*
 * Copyright (c) 2013 UCE (Universidad Central del Ecuador), todos los derechos
 * reservados. Este software es confidencial y su informacion es propiedad de
 * UCE (Universidad Central del Ecuador). No debe revelar tal informacion y debe usarla
 * unicamente de acorde con los terminos de uso.
 */
package ec.linde.modulos.recursos;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * TODO: Agregar descripcion
 * 
 * @FechaCreacion: 29/03/2013
 * @author: Alex Carrillo
 * @FechaModificacion: 29/03/2013
 * @author: Alex Carrillo
 * @version: 1.0
 */
@WebServlet("/image/*")
public class ImageServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {

			// Get image file.
			String file = request.getParameter("file");

			if (!file.isEmpty()) {

				BufferedInputStream in = null;
				in = new BufferedInputStream(new FileInputStream("D:" + File.separator + file));

				byte[] bytes = null;
				if (in != null) {
					// Get image contents.
					bytes = new byte[in.available()];
					in.read(bytes);
					in.close();

					// Write image contents to response.
					response.getOutputStream().write(bytes);
				}

			}

		} catch (FileNotFoundException e) {
			throw new FileNotFoundException("Archivo no encontrado");
		} catch (IOException e) {

			e.printStackTrace();

		}
	}
}
